import parse from '@dfeidao/atom-web/dt/parse';
import get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import hide from '@dfeidao/atom-web/ui/hide';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';
import { productid } from '../../atom/config';
import success from '@dfeidao/atom-web/msgbox/success';
import error from '@dfeidao/atom-web/msgbox/error';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	const flag = get('flag');

	// “考试类别 examination_category ”，界面“考试类别”下拉框回填的“考试类别”

	// “开始时间 start_time ”，界面“开始时间”时间戳的值（前一个）

	// “结束时间 finish_time ”，界面“结束时间”时间戳的值（后一个）

	// “试卷总分 exam_score  ”，界面“考试分值”输入框的值

	// “考试时长 exam_time ”，界面“考试时长”输入框的值

	// “考试说明 exam_explain ”，界面“考试说明”输入框的值
	const exam_name1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_name"]');
	const exam_name = exam_name1.value;
	const examination_category1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="examination_category"]');
	const examination_category = examination_category1.value;
	const exam_score1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_score"]');
	const exam_score = exam_score1.value;
	const exam_time1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_time"]');
	const exam_time = exam_time1.value;
	const exam_explain1 = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="exam_explain"]');
	const exam_explain = exam_explain1.value;
	const start_time12 = fd.data.node.querySelector<HTMLInputElement>('[data-id="start_time"]');
	const start_time1 = start_time12.value;
	const finish_time12 = fd.data.node.querySelector<HTMLInputElement>('[data-id="finish_time"]');
	const finish_time1 = finish_time12.value;
	const staff_no = get('staff_no');
	const staff_name = get('staff_name');
	const time = new Date();
	const release_time = time.getTime();
	const start_time=parse(start_time1);
	const finish_time=parse(finish_time1);
	const reg = new RegExp("^[0-9]*$");
	if (!reg.test(exam_time) || !reg.test(exam_score)) {
		error("考试分值和考试时长均为数字类型哦亲");
		return;
	 }
	 const p = new RegExp("^[a-zA-Z0-9]{0,50}$");
	if (!p.test(exam_explain)) {
		error("考试说明的长度为五十以内哦亲");
		return;
	 }
	if (!exam_name || !exam_score || !exam_time || !examination_category || !start_time1 || !finish_time1) {
		error("亲，您还有一些信息需要完善呢！");
		return;
	} else {
		const res = await nodejs<{code: 1|0;result: string}> ('examination-management/zj-000002/s001',{
			productid,
			exam_name,
			examination_category,
			start_time,
			finish_time,
			exam_score,
			exam_time,
			exam_explain,
			staff_no,
			staff_name,
			release_time
		});
		if (res.code === 1) {
			success("添加成功");
			const addtol=fd.data.node.querySelector<HTMLDivElement>('[data-id="show"]');
	// 修改display-n 为false 隐藏
			set_node_cls(addtol, 'display-n',true);
			hide(fd.data.node);
			fire('zj-000001','a007');
			if (flag === 1 ) {
				fire('zj-000001','a010');
			} else {
			fire('zj-000001','a002');
			}

		} else {
			alert("添加失败了哦" + res.result);
		}
	}
}
